<!DOCTYPE html>
<html  lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>发送命令</title>
    <script src="../../static/js/jquery.1.9.1.min.js"></script>
</head>
<body>
<div>
    <select id="single_object">
        {% for vnf in vnfLists %}
            <option value="{{ vnf.ip }}">{{ vnf.ip }}</option>
        {% endfor %}
    </select>
    <input type="text" name="single_content">
    <button id="single_chat">单聊</button>
</div>
<br>
<br>
<br>
<br>
<div>
    <select multiple id="group_object">
        {% for vnf in vnfLists %}
            <option value="{{ vnf.ip }}">{{ vnf.ip }}</option>
        {% endfor %}
    </select>
    <input type="text" name="group_content">
    <button id="group_chat">组聊</button>
</div>
<br>
<br>
<br>
<br>
<div>
    <input type="text" name="broadcast_content">
    <button id="broadcast_chat">群聊</button>
</div>
<script>
    var baseurl = '/web/';
    $(function(){
        $("#single_chat").click(function(){
            var chat_to = [];
            chat_to.push($("#single_object option:selected").val());
            var content = $("input[name=single_content]").val();
            console.log("chat_to:" + chat_to + "    content:" + content);
            $.ajax({
                type: "POST",
                url: baseurl + "send-command",
                data: JSON.stringify({chat_to:chat_to, content:content, command_id:102}),
                dataType: "json",
                contentType: "application/json",
                success: function(data){
                    if (data["code"] == 1){
                        $("input[name=single_content]").val("");
                        console.log(data["message"]);
                    }else
                    {
                        console.log(data["message"]);
                    }

                }
            });
        });

        $("#group_chat").click(function(){
            var chat_tos = [];
            var chat_to = $("#group_object  option:selected").each(function(){
                chat_tos.push($(this).val());
            });
            var content = $("input[name=group_content]").val();
            console.log("chat_to:" + chat_tos + "    content:" + content);
            $.ajax({
                type: "POST",
                url: baseurl + "send-command",
                data: JSON.stringify({chat_to:chat_tos, content:content, command_id:103}),
                dataType: "json",
                contentType: "application/json",
                success: function(data){
                    if (data["code"] == 1){
                        $("input[name=group_content]").val("");
                        console.log(data["message"]);
                    }else
                    {
                        console.log(data["message"]);
                    }
                }
            });
        });

        $("#broadcast_chat").click(function(){
            var chat_to = [];
            {% for vnf in vnfLists %}
                chat_to.push("{{ vnf.ip }}");
            {% endfor %}
            var content = $("input[name=broadcast_content]").val();
            console.log("content:" + content);
            $.ajax({
                type: "POST",
                url: baseurl + "send-command",
                data: JSON.stringify({chat_to:chat_to, content:content, command_id:104}),
                dataType: "json",
                contentType: "application/json",
                success: function(data){
                    if (data["code"] == 1){
                        $("input[name=broadcast_content]").val("");
                        console.log(data["message"]);
                    }else
                    {
                        console.log(data["message"]);
                    }

                }
            });
        });
    });

</script>
</body>
</html>